//
//  StarView.m
//  Project1
//
//  Created by 黄珂耀 on 15/7/21.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "StarView.h"

@implementation StarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self _createStarViews];
    }
    return self;
}

- (void)_createStarViews
{
    UIImage *grayImage = [UIImage imageNamed:@"gray"];
    UIImage *yellowImage = [UIImage imageNamed:@"yellow"];
    
    _grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, grayImage.size.width * 5, grayImage.size.height)];
    [_grayView setBackgroundColor:[UIColor colorWithPatternImage:grayImage]];
    _yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, yellowImage.size.width * 5, yellowImage.size.height)];
    [_yellowView setBackgroundColor:[UIColor colorWithPatternImage:yellowImage]];
    
    CGFloat scale = self.frame.size.height / yellowImage.size.height;
    
    _grayView.transform = CGAffineTransformMakeScale(scale, scale);
    _yellowView.transform = CGAffineTransformMakeScale(scale, scale);
    
    CGRect frame = _grayView.frame;
    frame.origin = CGPointZero;
    _grayView.frame = frame;
    _yellowView.frame = frame;
    
    [self addSubview:_grayView];
    [self addSubview:_yellowView];

}

- (void)setAverage:(float)average
{
    _average = average;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    float width = _average / 10.0 * _yellowView.frame.size.width;
    
    CGRect frame = _grayView.frame;
    frame.size.width = width;
    
    _yellowView.frame = frame;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
